import 'package:chat_app/widgets/message_bubble.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class ChatMessages extends StatelessWidget {
  const ChatMessages({super.key});

  @override
  Widget build(BuildContext context) {
    final authentificatedUser = FirebaseAuth.instance.currentUser!;
    return StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('chat')
            .orderBy(
              'createdAt',
              descending: true,
            )
            .snapshots(),
        builder: (ctx, chatSnapshots) {
          if (chatSnapshots.connectionState == ConnectionState.waiting) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }

          if (!chatSnapshots.hasData || chatSnapshots.data!.docs.isEmpty) {
            return Center(
              child: Text('No messages found'),
            );
          }

          if (chatSnapshots.hasError) {
            return Center(
              child: Text('Something went wrong...'),
            );
          }
          final loadedMessages = chatSnapshots.data!.docs;
          return ListView.builder(
              padding: const EdgeInsets.only(bottom: 40, left: 13, right: 13),
              reverse: true,
              itemCount: loadedMessages.length,
              itemBuilder: (context, index) {
                final chatMessages = loadedMessages[index].data();
                final nextChatMessage = index + 1 < loadedMessages.length
                    ? loadedMessages[index + 1].data()
                    : null;
final currentMessageUserId = chatMessages['userId'];
final nextMessageUserId = nextChatMessage != null ? nextChatMessage['userId']: null;
final nextUserIsSame = nextMessageUserId == currentMessageUserId;
if(nextUserIsSame){
  return MessageBubble.next(
    message: chatMessages['text'], 
    isMe: authentificatedUser.uid == currentMessageUserId);
} else{
  return MessageBubble.first(
    userImage: chatMessages['userImage'], 
    username: chatMessages['username'],
     message: chatMessages['text'], 
     isMe: authentificatedUser.uid == currentMessageUserId);
}
              });
        });
  }
}
